<template>
  <div>
    <el-dialog
      title="核销订单"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      append-to-body
      @close="close"
    >
      <el-form
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="订单编号：" prop="orderNo">
          <el-input
            v-model="form.orderNo"
            :disabled="true"
            maxlength="100"
          />
        </el-form-item>
        <el-form-item label="预约时间：" prop="orderNo">
          <el-input
            v-model="form.time"
            :disabled="true"
            maxlength="100"
          />
        </el-form-item>
        <el-form-item label="预约人：" prop="orderNo">
          <el-input
            v-model="form.name"
            :disabled="true"
            maxlength="100"
          />
        </el-form-item>
        <el-form-item label="预约号码：" prop="orderNo">
          <el-input
            v-model="form.phone"
            :disabled="true"
            maxlength="100"
          />
        </el-form-item>
        <el-form-item label="核销码：" prop="code">
          <el-input
            v-model="form.code"
            placeholder="请输入顾客提供的核销码"
            maxlength="100"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
        <el-button size="small" round :disabled="loading" type="primary" @click="submit">核销</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { writeOffOrder } from '@/api/reservation'
export default {
  data() {
    return {
      // 校验
      rules: {
        code: [{ required: true, message: '请输入核销码', trigger: 'change' }]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      detail: '',
      form: {
        orderNo: null,
        code: null
      }
    }
  },
  methods: {
    /**
       * 弹框相关
       */
    open({ detail }) {
      this.detail = JSON.parse(JSON.stringify(detail))
      this.form = detail
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.detail = ''
      this.form = {
        orderNo: null,
        code: null
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 数据组
       */

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$confirm(`确定要核销: ${this.form.orderNo}?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            writeOffOrder(this.form)
              .then((res) => {
                this.$notify({
                  title: '成功',
                  message: '核销成功',
                  type: 'success',
                  duration: 2000,
                  offset: 40
                })
                this.resolve && this.resolve()
                this.hide()
              })
              .catch((err) => {})
              .finally(() => {
                this.loading = false
              })
          })
        }
      })
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
